const tools = require('../../utils/tools')
Page({
  data: {
    part:'',
    keyword:[],
    moreKeyword:[],
    sympthomName:[]
  },

  async onLoad(options) {
    wx.showLoading({
      title: '分析中',
      mask:true
    })
    let arr = [options.keyword]
    const obj1 = await this.apiFunc('getMoreKeyword',options.part,options.keyword)
    const obj2 = await this.apiFunc('getSympthomName',options.part,options.keyword)
    const array1 = await tools.fixFn(arr,obj1.result.data)
    this.setData({
      part:options.part,
      keyword:arr,
      moreKeyword:array1,
      sympthomName:obj2.result.data
    })
    wx.hideLoading()
  },

  // 封装调用接口函数
  async apiFunc(name,part,keyword){
    const array = await wx.cloud.callFunction({
      name,
      data:{ part, keyword }
    })
    return array
  },

  // 点击更多关键字
  async handleMoreKey(e){
    wx.showLoading({
      title: '分析中',
      mask:true
    })
    const temp = [e.currentTarget.dataset.keyword]
    let arr1 = [...this.data.keyword,...temp]
    let str = arr1.toString().replace(/,/g,'、')
    let obj1 = await this.apiFunc('getMoreKeyword',this.data.part,str)
    if(!obj1.result.data){
      arr1 = [...temp,...this.data.keyword]
      str = arr1.toString().replace(/,/g,'、')
      obj1 = await this.apiFunc('getMoreKeyword',this.data.part,str)
    }
    if (obj1.result.data){
      let obj2 = await this.apiFunc('getSympthomName',this.data.part,str)
      const array = await tools.fixFn(arr1,obj1.result.data)
      this.setData({
        keyword:arr1,
        moreKeyword:array,
        sympthomName:obj2.result.data
      })
    }else{
      this.setData({
        keyword:arr1,
        moreKeyword:[],
        sympthomName:[]
      })
      wx.showToast({
        title: '分析失败',
        icon: 'error'
      })
    }
    wx.hideLoading()
  },

  // 点击疾病名称
  handleName(e){
    wx.navigateTo({
      url: '/pages/sympthom_name/sympthom_name?name='+e.currentTarget.dataset.name,
    })
  }
})